﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.Common;
using Mimoza.DBLayer.Transport;
using Mimoza.Transport;

namespace Mimoza.DBLayer.Transport.Client
{
    public class UpdatesRepository :
        ClientRepository<UpdateRecord>,
        IDBUpdateRepository
    {
        public UpdatesRepository()
        {
            UpdatesRepositoryTranferRequest.RegisterInTransport();
            RepositoryReply<UpdateRecord>.RegisterInTransport();
        }

        public UpdateRecord GetUpdate(Guid ID)
        {
            return MakeObjectRequest(
                    UpdatesRepositoryCustomRequest.CustomOperationType.GetUpdate, ID);
        }

        public IEnumerable<UpdateRecord> GetUpdates()
        {
            return MakeCollectionRequest<UpdateRecord>(
                    UpdatesRepositoryCustomRequest.CustomOperationType.GetUpdates);
        }

        override public void Insert(UpdateRecord update)
        {
            update = MakeObjectRequest(new UpdatesRepositoryTranferRequest(Operation.Insert, update));
            base.Insert(update);
        }

        override public void Update(UpdateRecord update)
        {
            update = MakeObjectRequest(new UpdatesRepositoryTranferRequest(Operation.Update, update));
            base.Update(update);
        }

        override public void Delete(UpdateRecord update)
        {
            update = MakeObjectRequest(new UpdatesRepositoryTranferRequest(Operation.Delete, update));
            base.Delete(update);
        }

        UpdateRecord MakeObjectRequest(UpdatesRepositoryCustomRequest.CustomOperationType oper,
            Guid argument)
        {
            UpdatesRepositoryCustomRequest customReq = new UpdatesRepositoryCustomRequest(oper, argument.ToString());
            return MakeObjectRequest(new UpdatesRepositoryTranferRequest(customReq));
        }

        UpdateRecord MakeObjectRequest(UpdatesRepositoryCustomRequest.CustomOperationType oper,
            String argument)
        {
            UpdatesRepositoryCustomRequest customReq = new UpdatesRepositoryCustomRequest(oper, argument);
            return MakeObjectRequest(new UpdatesRepositoryTranferRequest(customReq));
        }


        ClientCollectionEnumerable<TCollectionRecord> MakeCollectionRequest<TCollectionRecord>(UpdatesRepositoryCustomRequest.CustomOperationType oper)
            where TCollectionRecord : class, new()
        {
            UpdatesRepositoryCustomRequest customReq = new UpdatesRepositoryCustomRequest(oper);
            return MakeCollectionRequest<TCollectionRecord>(new UpdatesRepositoryTranferRequest(customReq));
        }
    }
}
